package cn.lg.soar.common.util.jackson.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.text.DecimalFormat;

/**
 * LocalDate序列化
 * @author luguoxiang
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
public class DecimalSerializer extends JsonSerializer<Number> {

    private final DecimalFormat format;

    public DecimalSerializer() {
        this(",###");
    }
    public DecimalSerializer(String pattern) {
        this(new DecimalFormat(pattern));
    }
    public DecimalSerializer(DecimalFormat format) {
        this.format = format;
    }

    @Override
    public void serialize(Number value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        gen.writeString(format.format(value));
    }

}
